package com.yunupmall.service.ford.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunupmall.api.fordVO.MembetVo;
import com.yunupmall.api.fordentity.FordMember;
import com.yunupmall.api.po.UserCounts;
import com.yunupmall.entity.admin.memberpojo.Member;
import com.yunupmall.mapper.FordMapper.FordMenberMapper;
import com.yunupmall.service.ford.service.FordMemberService;
import com.yunupmall.uits.SessionUtis;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FordMemberimpl extends ServiceImpl<FordMenberMapper,Member> implements FordMemberService {


    @Override
    @Transactional
    public String addMenberUsers(MembetVo membetVo) {
        if (ObjectUtil.isEmpty(membetVo.getPhone())){
            throw new RuntimeException("手机号为空");
        }
        if(StringUtils.isEmpty(membetVo.getPassword())){
            throw new RuntimeException("用户密码不能为空");
        }
        Member member = new Member();
        BeanUtils.copyProperties(membetVo,member);
        member.setStatics(0);
        member.setDeflag(0);
        member.setCreateTime(LocalDateTime.now());
        if (this.save(member)){
            return "注册成功";
        }else{
            throw new RuntimeException("注册失败");
        }
    }

    @Override
    public List<Member> checkingUsers(MembetVo membetVo) {
        return null;
    }

    @Override
    public Member LoginUsers(String phone, String password) {
        if (ObjectUtil.isEmpty(phone)||ObjectUtil.isEmpty(password)){
            throw new RuntimeException("用户信息或密码为空");
        }
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member::getPhone,phone);
        wrapper.eq(Member ::getPassword,password);
        wrapper.eq(Member ::getStatics,0);
        wrapper.eq(Member ::getDeflag,0);
        List<Member> list = this.list(wrapper);
        if (list.size() !=0){
            return list.get(0);
        }else {
            throw new RuntimeException("没有检索到用户信息或状态已停用");
        }
    }

    /**
     * 修改用户信息
     * @return
     */
    @Override
    @Transactional
    public String updateDate(Member member) {
        if (ObjectUtil.isEmpty(member)){
            throw new RuntimeException("参数为空");
        }
        Member member1 = (Member) SessionUtis.getSessionAttribute("member");
        if (ObjectUtil.isEmpty(member1)){
            throw new RuntimeException("用户信息为空");
        }
        LambdaUpdateWrapper<Member> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Member ::getId,member1.getId());
        if (this.update(member,wrapper)){
            return "修改成功";
        }else {
            throw new RuntimeException("修改失败");
        }
    }

    /**
     * 信息显示
     * @return
     */
    @Override
    public Member getList() {
        Member member = (Member) SessionUtis.getSessionAttribute("member");
        if (ObjectUtil.isEmpty(member)){
            throw new RuntimeException("没有用户信息");
        }
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member ::getId,member.getId());
        wrapper.eq(Member ::getDeflag,0);
        List<Member> list = this.list(wrapper);
        if (list.size() > 0){
            return list.get(0);
        }else {
            return null;
        }
    }

    /**
     * 分页查询数据
     * @param membetVo
     * @return
     */
    @Override
    public Page<Member> page(MembetVo membetVo) {
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(membetVo.getName())){
            wrapper.like(Member::getName,membetVo.getName());
        }
        if (ObjectUtil.isNotEmpty(membetVo.getSex())) {
            wrapper.eq(Member::getSex,membetVo.getSex());
        }
        Page<Member> memberPage = new Page<Member>(membetVo.getPage(), membetVo.getLimit());
        Page<Member> page = this.page(memberPage, wrapper);
        return page;
    }


}
